/**
 * Copyright (c) 2010, Casey Lai
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * 
 * Neither the name of the Cheetah Group nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

package org.cheetahworkflow.designer.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;


public class SequenceNode extends LogicNode implements IPropertySource {
	
	private static final long serialVersionUID = 237972233541489406L;
	
	protected IPropertyDescriptor[] descriptors = new IPropertyDescriptor[] {
            new TextPropertyDescriptor(PROP_CAPTION, "Caption"),
            new TextPropertyDescriptor(PROP_DESCRIPTION, "Description"),
            new TextPropertyDescriptor(PROP_CLASS, "Class"),
            new TextPropertyDescriptor(PROP_PARAMETER, "Parameter")};
	
	private String className = "";
	
	private String parameter = "";
	
	public SequenceNode() {}
	
	public SequenceNode(String caption, Point location, Dimension size, String description, String className, String parameter) {
		this.caption = caption;
		this.location = location;
		this.size = size;
		this.description = description;
		this.className = className;
		this.parameter = parameter;
	}

	@Override
	public void addOutgoingArrow(Arrow arrow) {
		outgoingArrow = arrow;
		fireStructureChange(PROP_OUTGOING, arrow);
	}
	
	@Override
	public void removeOutgoingArrow(Arrow arrow) {
		if (outgoingArrow != null) {
			outgoingArrow = null;
			fireStructureChange(PROP_OUTGOING, outgoingArrow);
		}
	}
	
	@Override
	public List<Arrow> getOutgoingArrow() {
		if (outgoingArrow != null) {
			List<Arrow> list = new ArrayList<Arrow>();
			list.add(outgoingArrow);
			return list;
		} else {
			return Collections.emptyList();
		}
	}
	
	@Override
	public RapidView getRapidView() {
		return new RapidView(this);
	}
	
	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		if (!this.className.equals(className)) {
			String oldValue = this.className;
			this.className = className;
			firePropertyChange(PROP_CLASS, oldValue, className);
		}
	}
	
	public String getParameter() {
		return parameter;
	}
	
	public void setParameter(String parameter) {
		if (!this.parameter.equals(parameter)) {
			String oldValue = this.parameter;
			this.parameter = parameter;
			firePropertyChange(PROP_PARAMETER, oldValue, parameter);
		}
	}

	@Override
	public Object getEditableValue() {
		return this;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		return descriptors;
	}

	@Override
	public Object getPropertyValue(Object id) {
		if (PROP_CAPTION.equals(id)) {
			return getCaption();
		} else if (PROP_DESCRIPTION.equals(id)) {
			return getDescription();
		} else if (PROP_CLASS.equals(id)) {
			return getClassName();
		} else if (PROP_PARAMETER.equals(id)) {
			return getParameter();
		}
		return null;
	}

	@Override
	public boolean isPropertySet(Object arg0) {
		return true;
	}

	@Override
	public void resetPropertyValue(Object arg0) {
		
	}

	@Override
	public void setPropertyValue(Object id, Object value) {
		if (PROP_CAPTION.equals(id)) {
			setCaption((String) value);
		} else if (PROP_DESCRIPTION.equals(id)) {
			setDescription((String) value);
		} else if (PROP_CLASS.equals(id)) {
			setClassName((String) value);
		} else if (PROP_PARAMETER.equals(id)) {
			setParameter((String) value);
		}
	}
}
